package GUI.añadido;

import GUI.Dialogos;
import GUI.modelosTablas.SearchBoxModel;
import GUI.modelosTablas.ServiciosPrestadosDefaultTableModel;
import base.Aviso;
import base.Estado;
import base.Presupuesto;
import base.Servicio;
import base.Tarea;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import negocio.Usuario;
import negocio.bd.BDAviso;
import negocio.bd.BDPresupuestos;
import negocio.bd.BDServicios;
import negocio.bd.BDTareas;

/**
 *
 * @author Efren
 */
public class AñadirAviso extends javax.swing.JFrame {
    private Tarea tarea;
    private boolean isFirst;
    ServiciosPrestadosDefaultTableModel modelo;
    private Usuario user;
    /**
     * Creates new form AñadirAviso
     */
    public AñadirAviso(Usuario temp,Tarea tarea) {
        this.user = temp;
        this.tarea = tarea;
        initComponents();
        setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/img/ico.png")));
        isFirst = true;
        this.comboServicios.setModel(new SearchBoxModel(comboServicios, BDServicios.obtenerTodos()));
        modelo = new ServiciosPrestadosDefaultTableModel(false);
        this.tablaPresupuestoAntiguo.setModel(new ServiciosPrestadosDefaultTableModel(false, BDServicios.obtenerUltimosPrevistos(tarea.getIdTarea())));
        this.tablaServiciosPresupuesto.setModel(modelo);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDialog1 = new javax.swing.JDialog();
        jPanel7 = new javax.swing.JPanel();
        jButton3 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jSpinner1 = new javax.swing.JSpinner();
        popupPresupuesto = new javax.swing.JPopupMenu();
        mnuBorrarFila = new javax.swing.JMenuItem();
        mnuBorrarUnidades = new javax.swing.JMenuItem();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jPanel2 = new javax.swing.JPanel();
        btnEnviar = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jPanel6 = new javax.swing.JPanel();
        comboServicios = new javax.swing.JComboBox();
        btnAñadirPresupuesto = new javax.swing.JButton();
        unidades = new javax.swing.JSpinner();
        jScrollPane6 = new javax.swing.JScrollPane();
        tablaServiciosPresupuesto = new javax.swing.JTable();
        jLabel6 = new javax.swing.JLabel();
        lblTotal = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tablaPresupuestoAntiguo = new javax.swing.JTable();

        jDialog1.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        jDialog1.setTitle("Seleccion número");

        jButton3.setText("Cancelar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton1.setText("Aceptar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jSpinner1.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(1), Integer.valueOf(1), null, Integer.valueOf(1)));

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel7Layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton3))
                    .addGroup(jPanel7Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton3)
                    .addComponent(jButton1))
                .addContainerGap())
        );

        javax.swing.GroupLayout jDialog1Layout = new javax.swing.GroupLayout(jDialog1.getContentPane());
        jDialog1.getContentPane().setLayout(jDialog1Layout);
        jDialog1Layout.setHorizontalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDialog1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jDialog1Layout.setVerticalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDialog1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        popupPresupuesto.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
            public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
                popupPresupuestoPopupMenuWillBecomeVisible(evt);
            }
        });

        mnuBorrarFila.setText("Borrar fila");
        mnuBorrarFila.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuBorrarFilaActionPerformed(evt);
            }
        });
        popupPresupuesto.add(mnuBorrarFila);

        mnuBorrarUnidades.setText("Quitar unidades");
        mnuBorrarUnidades.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuBorrarUnidadesActionPerformed(evt);
            }
        });
        popupPresupuesto.add(mnuBorrarUnidades);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Gestioneitor 2000 - Enviar Aviso");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Descripción del aviso", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 14))); // NOI18N

        jTextArea1.setColumns(20);
        jTextArea1.setLineWrap(true);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1)
                .addContainerGap())
        );

        btnEnviar.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        btnEnviar.setText("Enviar");
        btnEnviar.setPreferredSize(new java.awt.Dimension(100, 25));
        btnEnviar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEnviarActionPerformed(evt);
            }
        });
        jPanel2.add(btnEnviar);

        jButton2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jButton2.setText("Cerrar");
        jButton2.setPreferredSize(new java.awt.Dimension(100, 25));
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel2.add(jButton2);

        jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Presupuesto nuevo", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 14))); // NOI18N

        comboServicios.setEditable(true);
        comboServicios.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        comboServicios.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        comboServicios.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
            public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
                comboServiciosPopupMenuWillBecomeVisible(evt);
            }
        });

        btnAñadirPresupuesto.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        btnAñadirPresupuesto.setText("Añadir");
        btnAñadirPresupuesto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAñadirPresupuestoActionPerformed(evt);
            }
        });

        unidades.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        unidades.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(1), Integer.valueOf(1), null, Integer.valueOf(1)));

        tablaServiciosPresupuesto.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        tablaServiciosPresupuesto.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tablaServiciosPresupuesto.setComponentPopupMenu(popupPresupuesto);
        tablaServiciosPresupuesto.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane6.setViewportView(tablaServiciosPresupuesto);

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel6.setText("Total:");

        lblTotal.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        lblTotal.setText(" ");

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addComponent(jScrollPane6, javax.swing.GroupLayout.DEFAULT_SIZE, 446, Short.MAX_VALUE)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addComponent(comboServicios, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(unidades, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnAñadirPresupuesto))
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(comboServicios, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnAñadirPresupuesto)
                    .addComponent(unidades, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane6, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(lblTotal))
                .addContainerGap())
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Presupuesto antiguo", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 14))); // NOI18N

        tablaPresupuestoAntiguo.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(tablaPresupuestoAntiguo);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, 0))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void btnEnviarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEnviarActionPerformed
        // TODO add your handling code here:
        Presupuesto pre = new Presupuesto();
       pre.setIdPresupuesto(tarea.getIdTarea());
       pre.setFecha(new GregorianCalendar());
       pre.setIdTecnicoResponsable(this.user.getId());
       pre.aceptar();
       ArrayList<Servicio> spre = new ArrayList<>(this.tablaServiciosPresupuesto.getRowCount());
       for (int i = 0; i < this.tablaServiciosPresupuesto.getRowCount();i++){
           Servicio temp = BDServicios.buscar(BDServicios.buscarPorNombre,((String)tablaServiciosPresupuesto.getValueAt(i, 0)) , BDServicios.ASC, false)[0];
           temp.setUnidades((int)tablaServiciosPresupuesto.getValueAt(i, 1));
           spre.add(temp);
       }
       pre.setServiciosEstimados(spre.toArray(new Servicio[0]));
        BDPresupuestos.insertar(pre);
        
        Aviso a = new Aviso();
        a.setFecha(new GregorianCalendar());
        a.setDescripcion(this.jTextArea1.getText());
        a.setIdTarea(tarea.getIdTarea());
        boolean insertar = BDAviso.insertar(a);
        tarea.setEstado(Estado.ECONFIRMACION);
        BDTareas.actualizar(tarea);
        this.setVisible(false);
        
        if (insertar){
            Dialogos.mostrarDialogoInformacion("Añadido Correctamente.");
        } else {
            Dialogos.mostrarDialogoInformacion("Error en la inserción en la base datos.");
        }
        
        this.dispose();
    }//GEN-LAST:event_btnEnviarActionPerformed

    private void comboServiciosPopupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {//GEN-FIRST:event_comboServiciosPopupMenuWillBecomeVisible
        //Si es la primera vez que se despliega el combo, cargar datos.
        if (isFirst && (comboServicios.getModel() != null)) {
            isFirst = false;
            ((SearchBoxModel)comboServicios.getModel()).updateModel("");
        }
    }//GEN-LAST:event_comboServiciosPopupMenuWillBecomeVisible

    private void btnAñadirPresupuestoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAñadirPresupuestoActionPerformed
        // variable para determinar si existe el servicio en la tabla
        boolean existe = false;
        String combo = ((String) this.comboServicios.getSelectedItem());
        if (combo != null){
            ServiciosPrestadosDefaultTableModel modelo = (ServiciosPrestadosDefaultTableModel)this.tablaServiciosPresupuesto.getModel();
            Servicio serv =BDServicios.buscar(BDServicios.buscarPorNombre,combo, BDServicios.ASC, false)[0];
            for (int i = 0; i < modelo.getRowCount();i++){
                if (serv.getNombre().equalsIgnoreCase((String)modelo.getValueAt(i, 0))){
                    modelo.setValueAt((((int)modelo.getValueAt(i, 1))+(int)unidades.getValue()), i, 1);
                    existe=true;
                }
            }
            if (!existe){
                serv.setUnidades((int)unidades.getValue());
                modelo.añadirFila(serv);
            }
            this.tablaServiciosPresupuesto.setModel(modelo);
            this.lblTotal.setText(modelo.calcularCoste() + " €");
            this.unidades.setValue(1);
        }
    }//GEN-LAST:event_btnAñadirPresupuestoActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        jDialog1.setVisible(false);
        jDialog1.dispose();
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:

        int valueAt = (int) this.tablaServiciosPresupuesto.getValueAt(this.tablaServiciosPresupuesto.getSelectedRow(), 1);
        int menos = (int) jSpinner1.getValue();
        int res = valueAt - menos;
        if (res <= 0){
            int fila = this.tablaServiciosPresupuesto.getSelectedRow();
            if (fila != -1){
                ServiciosPrestadosDefaultTableModel m = ((ServiciosPrestadosDefaultTableModel)tablaServiciosPresupuesto.getModel());
                m.removeRow(fila);
                tablaServiciosPresupuesto.setModel(m);
            }
        } else {
            this.tablaServiciosPresupuesto.setValueAt(res, tablaServiciosPresupuesto.getSelectedRow(), 1);
        }
        this.lblTotal.setText(((ServiciosPrestadosDefaultTableModel)this.tablaServiciosPresupuesto.getModel()).calcularCoste() + " €");
        jDialog1.setVisible(false);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void mnuBorrarFilaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuBorrarFilaActionPerformed
        // TODO add your handling code here:
        int fila = this.tablaServiciosPresupuesto.getSelectedRow();
        if (fila != -1){
            ServiciosPrestadosDefaultTableModel m = ((ServiciosPrestadosDefaultTableModel)tablaServiciosPresupuesto.getModel());
            m.removeRow(fila);
            tablaServiciosPresupuesto.setModel(m);
            this.lblTotal.setText(((ServiciosPrestadosDefaultTableModel)this.tablaServiciosPresupuesto.getModel()).calcularCoste() + " €");
        }
    }//GEN-LAST:event_mnuBorrarFilaActionPerformed

    private void mnuBorrarUnidadesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuBorrarUnidadesActionPerformed
        // TODO add your handling code here:
        int fila = this.tablaServiciosPresupuesto.getSelectedRow();
        if (fila != -1){
            jDialog1.pack();
            jDialog1.setLocationRelativeTo(null);
            jDialog1.setVisible(true);
        }
    }//GEN-LAST:event_mnuBorrarUnidadesActionPerformed

    private void popupPresupuestoPopupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {//GEN-FIRST:event_popupPresupuestoPopupMenuWillBecomeVisible
        // TODO add your handling code here:
        if (this.tablaServiciosPresupuesto.getSelectedRow() == -1){
            popupPresupuesto.setVisible(false);
        }
    }//GEN-LAST:event_popupPresupuestoPopupMenuWillBecomeVisible

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(AñadirAviso.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(AñadirAviso.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(AñadirAviso.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(AñadirAviso.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new AñadirAviso().setVisible(true);
            }
        }); */
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAñadirPresupuesto;
    private javax.swing.JButton btnEnviar;
    private javax.swing.JComboBox comboServicios;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JDialog jDialog1;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane6;
    private javax.swing.JSpinner jSpinner1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JLabel lblTotal;
    private javax.swing.JMenuItem mnuBorrarFila;
    private javax.swing.JMenuItem mnuBorrarUnidades;
    private javax.swing.JPopupMenu popupPresupuesto;
    private javax.swing.JTable tablaPresupuestoAntiguo;
    private javax.swing.JTable tablaServiciosPresupuesto;
    private javax.swing.JSpinner unidades;
    // End of variables declaration//GEN-END:variables
}
